﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using ReflectionStudio.Core.Database;
using IntexReport.DataSource;
using ReflectionStudio.Core.FTPClient;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for DSTEXWBD14.xaml
    /// </summary>
    public partial class DSTEXWBD14 : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        DSTEXWBD14Data m_Data, m_DataX;
        public SETVV setUI
        {
            get
            {
                return (SETVV)this.TryFindResource("SETUI");
            }
        }
        public DataRow dwVKPOS;
        public DataRow dwVVLIEFSCH;
        public DataRow dwCOLOR;
        public DataRow dwWBBST;
        public DataSet dsParamaters;
        DSTEXWBD14Report m_rpt;
        ArrayList listVV06, ListColor, listBatchNo;
        #endregion
        public DSTEXWBD14()
        {
            InitializeComponent();
            this.Name = "入库单";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            dsParamaters = (DataSet)dicParamater["dsParamaters"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new DSTEXWBD14Data(dicParamater);
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
             /*SetUpFtp();
            btnLogFTP_Click(null, null);
           
            txtVKPAUFKRS.Text = "Z ";
            txtVKPAUFLFD.Text = "002030";
             * */

        }
        public void btnLogFTP_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                return;
                string ftpsite = txtFTPSite.Text.Trim();
                string ftpport = txtFTPPort.Text.Trim();
                string ftpuser = txtFTPUser.Text.Trim();
                CheckFtp(ftpsite, ftpport, ftpuser, txtFTPPwd, IsBelongs2Admin.IsChecked == true);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show("FTP登录信息有误。请联系你们的管理员。");
            }
            finally
            {
                ftpClient.Close();
            }
        }  
        private void GenerateReport(string PrintType)
        {
            try
            {
                if (!setUI.Tables.Contains("WBBST"))
                    return;
                if (setUI.Tables["WBBST"].Rows.Count == 0)
                    return;
                DataRow dwWBBST = setUI.Tables["WBBST"].Rows[0];
                DataRow dwVKPOS=setUI.VKPOS.NewVKPOSRow();
                
                dwVKPOS["VKPAUFLFD"] = m_Data.Right("000000" + this.txtVVLIEFSCH.Text, 6);
                dwVKPOS["VKPMEEH"] = cmbUoM.Text.ToString().Trim();
                dwVKPOS["VKPART"] = dwWBBST["WBBART"];
                dwVKPOS["VKPFARBE"] = dwWBBST["WBBFARBE"].ToString().Trim() + "-" + dwWBBST["WBBBESCHICH"].ToString().Trim();
                setUI.VKPOS.Rows.Add(dwVKPOS);
                m_rpt = new DSTEXWBD14Report(dicParamater);

                m_rpt.dsSource = setUI;
                m_rpt.PrintType = PrintType;
                m_rpt.ftpClient = ftpClient;
                if (m_Data.exAgent._app == null)
                {
                    m_Data.exAgent = new ExcelX.ExcelLib.ExcelAgent(dicParamater);
                }
                m_rpt.proxy = this.proxy;
                m_rpt.OracleDB = this.OracleDB;
                m_rpt.exAgent = m_Data.exAgent;
                m_rpt.WriteExcelReport("EXCEL");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "DirectPrint";
            GenerateReport(PrintType);
        }

      
        private void btnGetVVLIEF_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string intexidcard = m_Data.Right("000000" + this.txtVVLIEFSCH.Text, 6);
                if (intexidcard != "000000")
                {
                    LoadData(intexidcard); 
                } 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 

        private void LoadData(string intexidcard)
        {
            setUI.Clear();
            setUI.AcceptChanges();
            setUI.Merge(m_Data.BuildData(intexidcard));
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnPrintPreview_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
